package cn.aj.org.service.impl;

import cn.aj.basic.enu.ShopEnum;
import cn.aj.basic.service.impl.BaseServiceImpl;
import cn.aj.basic.utils.BaiduAuditUtils;
import cn.aj.basic.utils.EmailUtil;
import cn.aj.basic.vo.ShopCountVo;
import cn.aj.basic.vo.ShopEchartsVo;
import cn.aj.org.domain.Employee;
import cn.aj.org.domain.Shop;
import cn.aj.org.domain.ShopAuditLog;
import cn.aj.org.dto.ShopAuditLogDto;
import cn.aj.org.dto.ShopSettlementAginDto;
import cn.aj.org.mapper.EmployeeMapper;
import cn.aj.org.mapper.ShopAuditLogMapper;
import cn.aj.org.mapper.ShopMapper;
import cn.aj.org.service.IShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aj
 * @since 2022-11-18
 */
@Service
@Slf4j
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 判断店铺不能为空
        if(StringUtils.isEmpty(shop.getName())) {
            throw new RuntimeException("您输入的店铺名为空,请重新输入!");
        }
        // 判断店铺名是否存在
        Shop shopName = shopMapper.getByName(shop.getName());
        if (null != shopName) {
            throw new RuntimeException("您输入的店铺名已经存在,请重新输入!");
        }
        // 判断管理员
        Employee admin = shop.getAdmin();
        Employee adminName = employeeMapper.getByUsername(admin.getUsername());
        // 判断管理员是否存在且是否绑定了店铺
        if(null != adminName && adminName.getShopId() != null) {
            throw new RuntimeException("您输入的管理员已经绑定店铺,请重新输入!");
        }
        // 判断管理员是否存在
        if(null == adminName) { // 不存在就注册保存信息
            employeeMapper.add(admin);
        }
        // 百度审核,判断店铺名字是否
        if(!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new RuntimeException("您输入的店铺名称不合规,请重新输入!");
        }
        // 百度审核,判断店铺名字是否
        if(!BaiduAuditUtils.ImgCensor(shop.getLogo())) {
            throw new RuntimeException("您上传的店铺log图片存在违规,请重新上传!");
        }
        // 保存管理员id
        shop.setAdminId(admin.getId());
        // 保存店铺
        shopMapper.add(shop);
        // 保存店铺id
        admin.setShopId(shop.getId());
        // 员工表修改管理员信息
        employeeMapper.update(admin);
    }

    /**
    * @Title: auditPass
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/21 0021 19:50
    * @Parameters: [shopAuditLogDto]
    * @Return void
     * 店铺审核
    */
    @Override
    public void auditPass(ShopAuditLogDto shopAuditLogDto) {
        // 更改店铺状态
        Shop shop = new Shop();
        // 设置店铺id和状态,前端返回的参数也就是这2个参数
        shop.setId(shopAuditLogDto.getShopId());
        shop.setState(ShopEnum.ON_ACTIVATE.getCode()); // 已激活
        // 修改店铺信息
        shopMapper.update(shop);
        // 保存店铺审核日志
        ShopAuditLog shopAuditLog = new ShopAuditLog();
        shopAuditLog.setAuditId(1); // 没做登录先写s todo
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setShopId(shopAuditLogDto.getShopId());
        shopAuditLog.setNote(shopAuditLogDto.getNote());
        // 保存日志到数据库
        shopAuditLogMapper.add(shopAuditLog);
        // 发送邮件
        EmailUtil.sendEmail("审核通过", "982616996@qq.com",
                "<h1>审核通过,请点击下方链接进行激活</h1>+" +
                "http://localhost:8080/shop/activate/+shopAuditLogDto.getShopId()");
    }

    /**
    * @Title: activate
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/21 0021 22:39
    * @Parameters: [id]
    * @Return void
     * 用户激活接口
    */
    @Override
    public void activate(Long id) {
        Shop shop = new Shop();
        shop.setId(shop.getId()); // 设置店铺id
        shop.setState(ShopEnum.ON_ACTIVATE.getCode()); // 设置店铺状态
        shopMapper.update(shop); // 修改状态

    }

    /**
    * @Title: auditReject
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/21 0021 23:43
    * @Parameters: [shopAuditLogDto]
    * @Return void
     * 驳回
    */
    @Override
    public void auditReject(ShopAuditLogDto shopAuditLogDto) {
        // 修改店铺状态
        Shop shop = new Shop();
        shop.setState(ShopEnum.ROLL_REJECT.getCode());
        shop.setId(shopAuditLogDto.getShopId());
        // 修改店铺信息
        shopMapper.update(shop);
        // 保存日志
        ShopAuditLog auditLog = new ShopAuditLog();
        auditLog.setAuditId(2); // 没做登录先写s todo
        auditLog.setAuditTime(new Date());
        auditLog.setShopId(shopAuditLogDto.getShopId());
        auditLog.setNote(shopAuditLogDto.getNote());
        shopAuditLogMapper.add(auditLog);
        // 发邮件,用户重新填写注册信息
        EmailUtil.sendEmail("宠物只能商城审核失败通知",
                "982616996@qq.com",
                "<h1>审核失败,请点击下方链接进行修改</h1>"+
                        "http://localhost:8081/#/registerAgain?shopId="+shopAuditLogDto.getShopId());
    }

    /**
    * @Title: settlementAgain
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/22 0022 16:54
    * @Parameters: [shop]
    * @Return void
     * 店铺二次入驻业务判断
     *
    */
    @Override
    public void settlementAgain(ShopSettlementAginDto settlementAginDto) {
        if(!BaiduAuditUtils.TextCensor(settlementAginDto.getUsername())) {
            throw new RuntimeException("您输入的店铺名称不合规,请重新输入!");
        }

    }

    /**
    * @Title: loadEcharts
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/22 0022 22:56
    * @Parameters: []
    * @Return cn.aj.basic.vo.ShopEchartsVo
     * 报表简答展示
    */
    @Override
    public ShopEchartsVo loadEcharts() {
        List<ShopCountVo> list = shopMapper.loadEcharts();
        List<Integer> x = list.stream().map(ShopCountVo::getState).collect(Collectors.toList());
        List<Integer> y = list.stream().map(ShopCountVo::getNum).collect(Collectors.toList());
        return ShopEchartsVo.builder().x(x).y(y).build();
    }

//    @Override
//    public PageListUtil<Shop> queryByPage(BaseQuery query) {
//        // 获取店铺集合
//        PageList<Shop> pageList = super.queryByPage(query);
//        List<Shop> shopList = pageList.getRows();
//        // 获取到店铺的所有id集合
//        List<Long> shopIds = shopList.stream().map(Shop::getId).collect(Collectors.toList());
//        // 根据店铺id集合 查询这些店铺下的所有日志数据
//        List<ShopAuditLog> logList =  auditLogMapper.findByShopIds(shopIds);
//        // 需要将每个店铺下的日志组装一下 返回map key ：shopId vaule list<rizhi>
//        Map<Long, List<ShopAuditLog>> logMap = logList.stream().collect(Collectors.groupingBy(ShopAuditLog::getShopId));
//        shopList.forEach(shop->{
//            List<ShopAuditLog> shopAuditLogs = logMap.get(shop.getId());
//            shop.setAuditLogs(shopAuditLogs);
//        });
//        return pageList;
//    }
//
//    @Override
//    public PageListUtil<Shop> loadPage(BaseQuery baseQuery) {
//        PageListUtil<Shop> shopPageListUtil = super.loadPage(baseQuery);
//        List<Shop> shopPageListUtilRows = shopPageListUtil.getRows();
//        ShopAuditLog shopAuditLog = new ShopAuditLog();
//        ShopAuditLog list = shopAuditLogMapper.getById(shopAuditLog.getShopId());
//
//
//        return
//    }
}
